package com.trkj.tags;

import java.io.IOException;
import java.text.MessageFormat;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;

/**
 * 分页标签
 * list?curpage=1&pagesize=10&city=株洲&sex=帅哥
 * @author Administrator
 *
 */
public class PageTag extends SimpleTagSupport {
	private int curpage;//curpage
	private int pagesize;//pagesize
	private int total;
	private String url;//list
	private String where;//条件 city=株洲&sex=帅哥
	public int getCurpage() {
		return curpage;
	}
	public void setCurpage(int curpage) {
		this.curpage = curpage;
	}
	public int getPagesize() {
		return pagesize;
	}
	public void setPagesize(int pagesize) {
		this.pagesize = pagesize;
	}
	public int getTotal() {
		return total;
	}
	public void setTotal(int total) {
		this.total = total;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getWhere() {
		return where;
	}
	public void setWhere(String where) {
		this.where = where;
	}
	
	@Override
	public void doTag() throws JspException, IOException {
		super.doTag();
		System.out.println(curpage);
		String pattern = " <a href=''{0}?curpage={1}&pagesize={2}&{3}''>{4}</a>";
		//总页数
		int page = (total - 1) / pagesize + 1;
		//条件
		String condition = where == null ? "" : where;
		//第一页
		String first = MessageFormat.format(pattern, url,
				1, pagesize, condition, "第一页");
		//上一页
		String pre = null;
		if(curpage == 1){
			pre = " <a>上一页</a>";
		}else{
			pre = MessageFormat.format(pattern, url,
				curpage - 1, pagesize, condition, "上一页");
			System.out.println(pre);
		}
		//下一页
		String next = null;
		if(page == curpage){
			next = " <a>下一页</a>";
		}else{
			next = MessageFormat.format(pattern, url,
					curpage + 1, pagesize, condition, "下一页");
		}
		//最后一页
		String last = MessageFormat.format(pattern, url,
				page, pagesize, condition, "最后一页"); 
		
		//other
		String other = "<span> " + curpage + "/" + page + " 共有" + total + "条记录</span>";
		
		JspWriter out = this.getJspContext().getOut();
		out.println(first + pre + next + last + other);
	}
}	









